/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"

#define IS_KEY3_PRESSED()  (HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_9) == GPIO_PIN_RESET)
#define IS_KEY4_PRESSED()  (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_2) == GPIO_PIN_RESET)
#define IS_KEY5_PRESSED()  (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_1) == GPIO_PIN_RESET)
#define IS_KEY6_PRESSED()  (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_0) == GPIO_PIN_RESET)

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);

int current_period = 500; //2k
uint16_t pulse_percent = 50; //设置占空比
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIM4)
	{
		//__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1,pulse);

		//__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1,pulse);
		//pulse += 1000;
		//if(pulse >= 10000)
		//pulse = 0;
		//HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_7);
	}
}

//按照指定占空比和频率初始化
void TIM_PWM_START(void)
{
	htim4.Init.Period = current_period;

	HAL_TIM_PWM_Init(&htim4);

	if(HAL_TIM_Base_Start_IT(&htim4) != HAL_OK)
	{
		Error_Handler();
	}
	HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);

	//设置指定占空比
	__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1, (current_period * pulse_percent) / 100);
}

int main(void)
{
	int isKey3Pressed = 0;
	int isKey4Pressed = 0;
	int isKey5Pressed = 0;
	int isKey6Pressed = 0;

	/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
	HAL_Init();

	if(IS_ENGINEERING_BOOT_MODE())
	{
		/* Configure the system clock */
		SystemClock_Config();
	}

	/* Initialize all configured peripherals */
	MX_GPIO_Init();
	MX_TIM4_Init();

	//获取当前的周期
	current_period = htim4.Init.Period;
	pulse_percent = 50;

	//TIM_PWM_START();

	while (1)
	{
		HAL_Delay(1);
		if (isKey3Pressed == 0){
			if (IS_KEY3_PRESSED()){
				//消抖
				HAL_Delay(10);
				if (IS_KEY3_PRESSED()){
					isKey3Pressed = 1;

					HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_1);
				}
			}
		} else{
			//按下状态
			if (!IS_KEY3_PRESSED()){
				isKey3Pressed = 0;
				//松开事件

			}
		}


		if (isKey4Pressed == 0){
			if (IS_KEY4_PRESSED()){
				//消抖
				HAL_Delay(10);
				if (IS_KEY4_PRESSED()){
					isKey4Pressed = 1;
					//HAL_TIM_Base_Start(&htim4);
					current_period = 500; //默认值
					pulse_percent = 50;
					TIM_PWM_START();
				}
			}
		} else{
			//按下状态
			if (!IS_KEY4_PRESSED()){
				isKey4Pressed = 0;
			}
		}

		if (isKey5Pressed == 0){
			if (IS_KEY5_PRESSED()){
				//消抖
				HAL_Delay(10);
				if (IS_KEY5_PRESSED()){
					isKey5Pressed = 1;
					//按下了，改变占空比
					current_period += 100;
					if (current_period >= 1000){

						current_period = 100;
					}

					HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_1);
					TIM_PWM_START();
				}
			}
		} else{
			//按下状态
			if (!IS_KEY5_PRESSED()){
				isKey5Pressed = 0;
			}
		}

		if (isKey6Pressed == 0){
			if (IS_KEY6_PRESSED()){
				//消抖
				HAL_Delay(10);
				if (IS_KEY6_PRESSED()){
					isKey6Pressed = 1;
					//改变占空比
					pulse_percent += 10;
					if (pulse_percent >= 100){

						pulse_percent = 10;
					}

					HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_1);
					TIM_PWM_START();
				}
			}
		} else{
			//按下状态
			if (!IS_KEY6_PRESSED()){
				isKey6Pressed = 0;

			}
		}
	}

}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.HSIDivValue = RCC_HSI_DIV1;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL4.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** RCC Clock Config
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_ACLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_PCLK3|RCC_CLOCKTYPE_PCLK4
                              |RCC_CLOCKTYPE_PCLK5;
  RCC_ClkInitStruct.AXISSInit.AXI_Clock = RCC_AXISSOURCE_HSI;
  RCC_ClkInitStruct.AXISSInit.AXI_Div = RCC_AXI_DIV1;
  RCC_ClkInitStruct.MCUInit.MCU_Clock = RCC_MCUSSOURCE_HSI;
  RCC_ClkInitStruct.MCUInit.MCU_Div = RCC_MCU_DIV1;
  RCC_ClkInitStruct.APB4_Div = RCC_APB4_DIV1;
  RCC_ClkInitStruct.APB5_Div = RCC_APB5_DIV1;
  RCC_ClkInitStruct.APB1_Div = RCC_APB1_DIV1;
  RCC_ClkInitStruct.APB2_Div = RCC_APB2_DIV1;
  RCC_ClkInitStruct.APB3_Div = RCC_APB3_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

